package com.tarena.csmall.order.web.repository;

import cn.tedu.mall.pojo.order.vo.CartStandardVO;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class CartRepositoryImpl implements CartRepository {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    //更新一个用户的某个购物车商品sku的quantity数量
    //redis命令 hset userId skuId quantity
    //定义购物车数据 前缀
    public static final String CART_PREFIX="cart:";
    @Override public void updateCartNum(Long userId, Long skuId, Integer quantity) {
        //那到操作hash的对象
        HashOperations<String, Object, Object> opsForHash = stringRedisTemplate.opsForHash();
        opsForHash.put(CART_PREFIX+userId,skuId+"",quantity+"");
    }
    //hset userId skuId quantity
    @Override public void addCart(Long userId, Long skuId, Integer quantity) {
        HashOperations<String, Object, Object> opsForHash = stringRedisTemplate.opsForHash();
        opsForHash.put(CART_PREFIX+userId,skuId+"",quantity+"");
    }
    //检查一下当前userId下,hash类型数据中保存的cart购物车sku个数
    //hkeys userId 返回一个集合,计算获取集合size
    //是否到达临界值20
    @Override public Integer getCartNum(Long userId) {
        //拿到操作hash对象
        HashOperations<String, Object, Object> opsForHash = stringRedisTemplate.opsForHash();
        String cartKey=CART_PREFIX+userId;
        Long size = opsForHash.size(cartKey);
        return size.intValue();
    }

    @Override public Integer getCartQuantity(Long userId, Long skuId) {
        //hget userId skuId 如果有 返回数字quantity 如果没有 返回 null
        //拿到操作hash对象
        HashOperations<String, Object, Object> opsForHash = stringRedisTemplate.opsForHash();
        String cartKey=CART_PREFIX+userId;
        String quantityStr = (String) opsForHash.get(cartKey, skuId + "");
        // 之所以报错,是在处理数据逻辑时候忽略str有可能是空
        if (quantityStr==null){
            return null;
        }
        return Integer.parseInt(quantityStr);
    }

    @Override public List<CartStandardVO> getMyCarts(Long userId) {
        List<CartStandardVO> carts=new ArrayList<>();
        //用userId生成 cartKey
        String cartKey=CART_PREFIX+userId;
        //拿到操作hash对象 field value全部都需
        HashOperations<String, Object, Object> opsForHash = stringRedisTemplate.opsForHash();
        //所有key值 hkeys  skuIds
        Set<Object> hashKeys = opsForHash.keys(cartKey);
        //便利,然后封装所有返回数据
        for (Object key : hashKeys) {
            String skuId=key.toString();
            CartStandardVO cart=new CartStandardVO();
            // 没有远程调用的时候,可以封装的是4个属性 id userId skuId quantity
            cart.setId(userId+":"+skuId);
            cart.setUserId(userId);
            cart.setSkuId(Long.parseLong(skuId));
            cart.setQuantity(Integer.parseInt((String) opsForHash.get(cartKey,skuId)));
            carts.add(cart);
        }
        return carts;
    }

    @Override public void deleteOrderCart(Long userId, List<Long> skuIds) {
        //使用userId找到购物车的key
        //skuIds删除的field目标
        //底层命令 hdel userId skuId1 skuId2...
        //用userId生成 cartKey
        String cartKey=CART_PREFIX+userId;
        //拿到操作hash对象 field value全部都需
        List<String> skuStrs=new ArrayList<>();
        for (Long id : skuIds) {
            skuStrs.add(id+"");
        }
        HashOperations<String, Object, Object> opsForHash = stringRedisTemplate.opsForHash();
        opsForHash.delete(cartKey,skuStrs.toArray());
    }
}
